﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAnB
{
    public partial class MainAdmin : Form
    {
        static ketnoi kn = new ketnoi();
        static DataTable dtNhanVien = new DataTable();
        static DataTable dtThucPham = new DataTable();
        static DataTable dtLoaiThucPham = new DataTable();
        static DataTable dtBan = new DataTable();
        static DataTable dtHoaDon = new DataTable();
        static DataTable dtLoaiHoaDon = new DataTable();

        public MainAdmin()
        {
            InitializeComponent();

        }
        private void MainAdmin_Load(object sender, EventArgs e)
        {
            // Nhan Vien Tab
            load_NhanVien();
            // Thuc Pham Tab
            load_ThucPham(99);
            load_LoaiThucPham();
            // Ban An Tab
            load_BanAn();
            // Thong Ke Hoa Đon Tab
            load_LoaiHoaDon();


        }
        private void load_NhanVien()
        {
            string sqlNV = "select * from NguoiDung Where isAdmin = 0";
            dtNhanVien = kn.getDataTable(sqlNV);
            lsNhanVien.DataSource = dtNhanVien;
            lsNhanVien.DisplayMember = "TenNV";
            lsNhanVien.ValueMember = "MaNV";
            //lsNhanVien.SelectedIndex = 0;
            lblSoLuongNV.Text = dtNhanVien.Rows.Count.ToString();

        }

        private void listBoxNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DataRow i in dtNhanVien.Rows)
            {
                if (lsNhanVien.SelectedValue.ToString() == i["MaNV"].ToString())
                {
                    txtHoTen.Text = i["TenNV"].ToString();
                    txtCMND.Text = i["CMND"].ToString();
                    txtDiaChi.Text = i["DiaChi"].ToString();
                    lblMaNV.Text = i["MaNV"].ToString();
                    txtSDT.Text = i["SoDienThoai"].ToString();
                    dtpNgaySinh.Value = (DateTime)i["NgaySinh"];
                    dtpNgayLam.Value = (DateTime)i["NgayLam"];
                    if ((bool)i["isNam"]) radioButton1.Checked = true;
                    else radioButton2.Checked = true;
                }
            }
        }

        private void btThemNV_Click(object sender, EventArgs e)
        {
            frmDangKiNV f = new frmDangKiNV();
            if (f.ShowDialog() == DialogResult.OK)
                load_NhanVien();
        }

        private void MainAdmin_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = MessageBox.Show("Bạn thật sự muốn thoát chứ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DialogResult == DialogResult.Yes)
            {
                Session._MaNV = 0;
                Session._TenNV = "";
            }
            else e.Cancel = true;
        }

        private void btCapNhatNV_Click(object sender, EventArgs e)
        {
            bool isNam = (true) ? radioButton1.Checked : radioButton2.Checked;
            string sql = "Update NguoiDung Set TenNV=N'" + txtHoTen.Text + "', DiaChi=N'" + txtDiaChi.Text + "', CMND=N'" + txtCMND.Text + "', SoDienThoai=N'" + txtSDT.Text + "',NgaySinh='" + dtpNgaySinh.Value.ToString("yyyy-MM-dd") + "',NgayLam = '" + dtpNgayLam.Value.ToString("yyyy-MM-dd") + "', isNam = '" + isNam + "' Where MaNV = " + lsNhanVien.SelectedValue.ToString();
            kn.ExeCuteNonQuery(sql);
            load_NhanVien();
            MessageBox.Show("Cập nhật " +txtHoTen.Text + " thành công","Thông báo", MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void btXoaNV_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa: " + txtHoTen.Text, "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string sql = "Delete from NguoiDung where MaNV = '" + lsNhanVien.SelectedValue.ToString() + "'";
                try
                {
                    kn.ExeCuteNonQuery(sql);
                }
                catch (Exception ex)
                {

                    MessageBox.Show("Looisdkjfghskjgdkjh");
                }
              
                load_NhanVien();
            }
        }


        private void load_ThucPham(int loai)
        {
            dtThucPham.Clear();
            string sql;
            if (loai != 99) sql = "Select * from ThucPham Where MaLoaiThucPham =" + loai.ToString();
            else
                sql = "Select * from ThucPham";
            dtThucPham = kn.getDataTable(sql);
            lsThucPham.DataSource = dtThucPham;
            lsThucPham.DisplayMember = "Ten";
            lsThucPham.ValueMember = "ID";
            kn.disconnect();
            lblSoLuongTP.Text = dtThucPham.Rows.Count.ToString();
        }

        private void load_LoaiThucPham()
        {
            string sql = "Select * from LoaiThucPham";
            dtLoaiThucPham = kn.getDataTable(sql);
            cbbLoaiThucPham.DataSource = dtLoaiThucPham;
            cbbLoaiThucPham.DisplayMember = "LoaiThucPham";
            cbbLoaiThucPham.ValueMember = "MaLoaiThucPham";

            DataTable dtLLTP = kn.getDataTable(sql);
            cbbLocLoaiThucPham.DataSource = dtLLTP;
            DataRow all = dtLLTP.NewRow();
            all[0] = 99;
            all[1] = "----- Tất cả -----";
            dtLLTP.Rows.InsertAt(all, 0);

            cbbLocLoaiThucPham.DisplayMember = "LoaiThucPham";
            cbbLocLoaiThucPham.ValueMember = "MaLoaiThucPham";

            cbbLocLoaiThucPham.SelectedIndex = 0;

        }

        private void lsThucPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DataRow i in dtThucPham.Rows)
            {
                if (lsThucPham.SelectedValue.ToString() == i["ID"].ToString())
                {
                    lblMaThucPHam.Text = i["ID"].ToString();
                    txtTenThucPham.Text = i["Ten"].ToString();
                    txtDonGia.Text = i["Gia"].ToString();
                    txtDonViTinh.Text = i["DonVi"].ToString();
                    cbbLoaiThucPham.SelectedValue = i["MaLoaiThucPham"].ToString();
                }
            }
        }



        private void cbbLocLoaiThucPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_ThucPham((int)cbbLocLoaiThucPham.SelectedValue);

        }



        private void MainAdmin_Shown(object sender, EventArgs e)
        {
            //cbbLocLoaiThucPham.SelectedIndex = 0;
            //lsNhanVien.SelectedIndex = 0;

        }


        private void btnThemTP_Click(object sender, EventArgs e)
        {
            frmThemThucPham f = new frmThemThucPham();
            if (f.ShowDialog() == DialogResult.OK)
                load_ThucPham((int)cbbLocLoaiThucPham.SelectedValue); ;

        }

        private void btnCapNhatTP_Click(object sender, EventArgs e)
        {
            string sql = "Update ThucPham Set Ten=N'" + txtTenThucPham.Text + "', Gia='" + txtDonGia.Text + "', DonVi=N'" + txtDonViTinh.Text + "', MaLoaiThucPham = " + cbbLoaiThucPham.SelectedValue.ToString() + " Where ID = " + lsThucPham.SelectedValue.ToString();
            kn.ExeCuteNonQuery(sql);
            MessageBox.Show("Cập nhật thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            load_ThucPham((int)cbbLocLoaiThucPham.SelectedValue);
        }

        private void btnXoaTP_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa: " + txtTenThucPham.Text, "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string sql = "Delete from ThucPham where ID = " + lsThucPham.SelectedValue.ToString();
                kn.ExeCuteNonQuery(sql);
                load_ThucPham((int)cbbLocLoaiThucPham.SelectedValue);
            }



        }

       
        private void load_BanAn()
        {
            string sql = "Select * from Ban";
            dtBan = kn.getDataTable(sql);
            lsBan.DataSource = dtBan;
            lsBan.DisplayMember = "TenBan";
            lsBan.ValueMember = "ID";
            lblSoLuongBanAn.Text = dtBan.Rows.Count.ToString();
        }

        private void lsBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DataRow i in dtBan.Rows)
            {
                if (i["ID"].ToString() == lsBan.SelectedValue.ToString())
                {
                    lblMaBan.Text = i["ID"].ToString();
                    txtTenBan.Text = i["TenBan"].ToString();
                }
            }
        }

        private void btnThemBan_Click(object sender, EventArgs e)
        {
            frmThemBanAn f = new frmThemBanAn();
            if (f.ShowDialog() == DialogResult.OK)
                load_BanAn();
        }

        private void btnCapNhatBan_Click(object sender, EventArgs e)
        {
            string sql = "Update Ban Set TenBan=N'" + txtTenBan.Text + "' Where ID = " + lsBan.SelectedValue.ToString();
            kn.ExeCuteNonQuery(sql);
            MessageBox.Show("Cập nhật bàn ăn thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            load_BanAn();
        }

        private void btnXoaBan_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa: " + txtTenBan.Text, "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string sql = "Delete from Ban where ID = " + lsBan.SelectedValue.ToString();
                kn.ExeCuteNonQuery(sql);
                load_BanAn();
            }
        }
        private void load_LoaiHoaDon()
        {
            lblMaHD.Text = String.Empty; 
            lblTongTien.Text = String.Empty;
            lblThoiGian.Text = String.Empty;
            lblTenNV.Text = String.Empty;
            lblTenBan.Text = String.Empty;
            dgChiTietHD.DataSource = null;
            string sql = String.Empty;

            if (rbtnTheoNam.Checked)
                sql = "SELECT distinct CONVERT(Varchar, YEAR(NgayBan)) AS 'Display', convert(varchar(4),NgayBan, 120)  AS 'Value' FROM HoaDon";
            if (rbtnTheoThang.Checked)
                sql = "SELECT distinct CONVERT(Varchar, MONTH(NgayBan)) + '-' + CONVERT(Varchar, YEAR(NgayBan)) AS Display,convert(varchar(7),NgayBan, 120) AS Value  FROM HoaDon";
            if (rbtnTheoNgay.Checked)
                sql = "SELECT distinct CONVERT(varchar, Day(NgayBan)) + '-'+ CONVERT(Varchar, MONTH(NgayBan)) + '-' + CONVERT(Varchar, YEAR(NgayBan)) AS Display, convert(varchar(10),NgayBan, 120) AS Value FROM HoaDon";
            if (rbtnTheoNhanVien.Checked)
                sql = "SELECT TenNV as 'Display', CONVERT(varchar,MaNV) as 'Value' from NguoiDung Where isAdmin = 0";
            if (rbtnTheoBan.Checked)
                sql = "Select TenBan as 'Display',  CONVERT(varchar,ID) as 'Value' from Ban";

            dtLoaiHoaDon = kn.getDataTable(sql);
            cbbLoaiHoaDon.DataSource = dtLoaiHoaDon;
            DataRow all = dtLoaiHoaDon.NewRow();
            all["Value"] = "all";

            all["Display"] = "-- Tất Cả --";
            dtLoaiHoaDon.Rows.InsertAt(all, 0);

            cbbLoaiHoaDon.DisplayMember = "Display";
            cbbLoaiHoaDon.ValueMember = "Value";
            cbbLoaiHoaDon.SelectedIndex = 0;
        }

        private void btnXoaHoaDon_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa?", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string sql = "Delete from HoaDon where MaHoaDon = " + lsHoaDon.SelectedValue.ToString();
                string sql2 = "Delete from ChiTietHoaDon where MaHoaDon = " + lsHoaDon.SelectedValue.ToString();
                kn.ExeCuteNonQuery(sql2);
                kn.ExeCuteNonQuery(sql);
                load_lsHoaDon();
            }
        }

        private void load_lsHoaDon()
        {
            lblMaHD.Text = "Không có";
            lblTongTien.Text = String.Empty;
            lblThoiGian.Text = String.Empty;
            lblTenNV.Text = String.Empty;
            lblTenBan.Text = String.Empty;
            dgChiTietHD.DataSource = null;
            string sql = String.Empty;
            if (cbbLoaiHoaDon.SelectedValue.ToString() == "all")
            {
                sql = "select * from HoaDon";
            }
            else
            {
                if (rbtnTheoNgay.Checked || rbtnTheoNam.Checked || rbtnTheoThang.Checked)
                    sql = "select * From HoaDon Where NgayBan like '" + cbbLoaiHoaDon.SelectedValue.ToString() + "%'";
                if (rbtnTheoNhanVien.Checked)
                    sql = "select * from HoaDon Where MaNV =" + cbbLoaiHoaDon.SelectedValue.ToString();
                if (rbtnTheoBan.Checked)
                    sql = "select * from HoaDon Where MaBan =" + cbbLoaiHoaDon.SelectedValue.ToString();
            }
            //MessageBox.Show("dt ls HoaDon change");
            dtHoaDon = kn.getDataTable(sql);
            lsHoaDon.DataSource = dtHoaDon;
            lsHoaDon.DisplayMember = "MaHoaDon";
            lsHoaDon.ValueMember = "MaHoaDon";
            lblSoLuongHD.Text = dtHoaDon.Rows.Count.ToString();
        }
        private void cbbLoaiThongKeHoaDon_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_lsHoaDon();
        }
        private void load_ChiTietHoaDon()
        {
            //lblMaHD.Text = String.Empty;
            //lblTongTien.Text = String.Empty;
            //lblThoiGian.Text = String.Empty;
            //lblTenNV.Text = String.Empty;
            //lblTenBan.Text = String.Empty;
            //dgChiTietHD.DataSource = null;

            string sql = String.Empty;
            foreach (DataRow i in dtHoaDon.Rows)
            {
                if (i["MaHoaDon"].ToString() == lsHoaDon.SelectedValue.ToString())
                {
                    ketnoi data = new ketnoi();
                    lblMaHD.Text = i["MaHoaDon"].ToString();
                    lblTongTien.Text = ((Double)i["GiaTri"]).ToString("C");
                    lblThoiGian.Text = ((DateTime)i["NgayBan"]).ToString("dd-MM-yyyy");
                    sql = "Select TenNV from NguoiDung Where MaNV =" + i["MaNV"].ToString();
                    lblTenNV.Text = kn.getDataTable(sql).Rows[0][0].ToString();
                    sql = "Select TenBan from Ban Where ID = " + i["MaBan"].ToString();
                    lblTenBan.Text = kn.getDataTable(sql).Rows[0][0].ToString();
                }
            }
            sql = "Select B.Ten AS 'Tên Món',B.DonVi As 'ĐVT', A.SoLuongDoDung AS 'Số Lượng'  ,B.Gia AS 'Đơn Giá', B.Gia * A.SoLuongDoDung AS 'Thành Tiền' from ChiTietHoaDon A, ThucPham B Where A.MaSanPham = B.ID AND A.MaHoaDon = " + lsHoaDon.SelectedValue.ToString();
            dgChiTietHD.DataSource = kn.getDataTable(sql);
        }
        private void lsThongKeHoaDon_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_ChiTietHoaDon();
        }

        private void rbtnTheoNhanVien_CheckedChanged(object sender, EventArgs e)
        {
            load_LoaiHoaDon();
        }

        private void rbtnTheoNam_CheckedChanged(object sender, EventArgs e)
        {
            load_LoaiHoaDon();
        }

        private void rbtnTheoThang_CheckedChanged(object sender, EventArgs e)
        {
            load_LoaiHoaDon();
        }

        private void rbtnTheoNgay_CheckedChanged(object sender, EventArgs e)
        {
            load_LoaiHoaDon();
        }

        private void rbtnTheoBan_CheckedChanged(object sender, EventArgs e)
        {
            load_LoaiHoaDon();
        }



    }
}
